package zxing.activity.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

import io.virtualapp.R;

/**
 * Created by Administrator on 2018/4/2 0002.
 */

public class SoundRecordView extends View {

    private int circleCenter; // 圆心
    private int circleWidth = px2dip(getContext(), 210);  //圆直径

    private int roundWidth;   //环宽度
    private int radius;       //内圆半径
    private RectF oval;
    private Paint mPaintText;
    private Paint mPaintText1;
    private float mProgress = 50;
    private int maxColorNumber = 50; //刻度数量
    private Paint paint;
    private int textColor;
    private float textSize = 40;
    private int roundBackgroundColor;

    private SweepGradient sweepGradient;

    private float singlPoint = 9;
    private float lineWidth = 2f;
    private String textValue = "00:00";
    private int[] colors = {getResources().getColor(R.color.colo7), getResources().getColor(R.color.colo8), getResources().getColor(R.color.colo9), Color.BLUE};
    int mLevel = 4;

    //    private ArrayList<Bitmap> bitmapArrayList;
//    private int[] iconSounds = {R.mipmap.mic_1, R.mipmap.mic_2, R.mipmap.mic_3, R.mipmap.mic_4,
//            R.mipmap.mic_5, R.mipmap.mic_6, R.mipmap.mic_7, R.mipmap.mic_8,
//            R.mipmap.mic_9, R.mipmap.mic_10,
//    };
    String time = "00:00";

    public SoundRecordView(Context context) {
        super(context);
        init();
    }

    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }



    public SoundRecordView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public static int px2dip(Context context, int dp) {
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5);

    }

    public SoundRecordView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        roundWidth = px2dip(getContext(), 20);


        circleCenter = circleWidth / 2;
        radius = (int) (circleCenter - roundWidth / 2); // 圆环的半径
        singlPoint = (float) 360 / (float) maxColorNumber;

        roundBackgroundColor = getResources().getColor(R.color.colo_10);
        textColor = Color.WHITE;
        sweepGradientInit();
//        setSweepGradient(colors)
        mPaintText = new Paint();
        mPaintText.setColor(textColor);
        mPaintText.setTextAlign(Paint.Align.CENTER);
        mPaintText.setTextSize(textSize);
        mPaintText.setAntiAlias(true);

        mPaintText1 = new Paint();
        mPaintText1.setColor(textColor);
        mPaintText1.setTextAlign(Paint.Align.CENTER);
        mPaintText1.setTextSize(textSize);
        mPaintText1.setAntiAlias(true);
        paint = new Paint();
        paint.setColor(roundBackgroundColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(roundWidth);
        paint.setAntiAlias(true);
//        Bitmap bitmap = C
//        bitmapArrayList = new ArrayList<>(10);
        Drawable drawable;
        TextView textView;
//        for (int i = 0; i < 10; i++) {
//            drawable = ContextCompat.getDrawable(getContext(), iconSounds[i]);
//            bitmapArrayList.add(((BitmapDrawable) drawable).getBitmap());
//        }

        // 用于定义的圆弧的形状和大小的界限
        oval = new RectF(circleCenter - radius, circleCenter - radius, circleCenter + radius, circleCenter + radius);
    }

    public void setTextColor(int color) {
        textColor = color;
        mPaintText.setColor(textColor);
        postInvalidate();
    }

    public void setTextSize(int size) {
        textSize = size;
        mPaintText.setTextSize(textSize);
        postInvalidate();
    }


    public void setProgress(int progress) {
        mProgress = progress;
        mLevel = progress / 10;
        mLevel = mLevel >= 10 ? 9 : mLevel;

        postInvalidate();
    }

    public void setTime(String t) {
        textValue = t;
        postInvalidate();
    }

    public void setSweepGradient(int[] colorArray) {
        colors = colorArray;
        //渐变颜色
        sweepGradient = new SweepGradient(this.circleWidth / 2, this.circleWidth / 2, colors, null);
        //旋转 不然是从0度开始渐变
        Matrix matrix = new Matrix();
        matrix.setRotate(-90, this.circleWidth / 2, this.circleWidth / 2);
        sweepGradient.setLocalMatrix(matrix);
        postInvalidate();
    }

    /**
     * 渐变初始化
     */
    public void sweepGradientInit() {
        //渐变颜色
        sweepGradient = new SweepGradient(this.circleWidth / 2, this.circleWidth / 2, colors, null);
        //旋转 不然是从0度开始渐变
        Matrix matrix = new Matrix();
        matrix.setRotate(-90, this.circleWidth / 2, this.circleWidth / 2);
        sweepGradient.setLocalMatrix(matrix);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measure(widthMeasureSpec), measure(heightMeasureSpec));
    }

    private int measure(int origin) {
        int specMode = MeasureSpec.getMode(origin);
        int specSize = MeasureSpec.getSize(origin);
        if (specMode == MeasureSpec.EXACTLY) {
            circleWidth = specSize;
        } else {
            if (specMode == MeasureSpec.AT_MOST) {
                circleWidth = Math.min(circleWidth, specSize);
            }
        }
        return circleWidth;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBackgroundScale(canvas);
        drawScale(canvas);
        canvas.drawText(textValue, circleCenter, circleCenter - getTextHeight(textValue, mPaintText), mPaintText);
        canvas.drawText("录制时间", circleCenter, circleCenter + getTextHeight(textValue, mPaintText), mPaintText1);
//        canvas.drawText(, circleCenter, circleCenter - getTextHeight(textValue, mPaintText), mPaintText);
    }

    /**
     * 画背景刻度
     *
     * @param canvas
     */
    private void drawBackgroundScale(Canvas canvas) {
        int p = maxColorNumber;
        float start = -90f;
        paint.setShader(null);
        for (int i = 0; i < p; i++) {
            canvas.drawArc(oval, start + singlPoint - lineWidth, lineWidth, false, paint); // 绘制间隔快
            start = (start + singlPoint);
        }
    }

    /**
     * 画进度刻度
     *
     * @param canvas
     */
    private void drawScale(Canvas canvas) {
        canvas.save();
        int p = (int) (mProgress / 100f * maxColorNumber);
        float start = -90f;
        for (int i = 0; i < p; i++) {
            paint.setShader(sweepGradient);
            canvas.drawArc(oval, start + singlPoint - lineWidth, lineWidth, false, paint); // 绘制间隔快
            start = (start + singlPoint);
        }
        canvas.restore();
    }


    private int getTextWidth(String text, Paint p) {
        Rect rect = new Rect();
        p.getTextBounds(text, 0, text.length(), rect);
        return rect.width();
    }

    private int getTextHeight(String text, Paint p) {
        Rect rect = new Rect();
        p.getTextBounds(text, 0, text.length(), rect);
        return rect.height();
    }

    private int getDpValue(int w) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, w, getContext().getResources().getDisplayMetrics());
    }


}
